<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #app {
            display: flex;
            justify-content: space-between;
        }

        .left {
            margin-left: 150px;
            margin-top: 100px;
        }

        .first {
            width: 500px;

        }

        .right {
            width: 600px;
            color: #e38a0e;
            height: 500px;
            display: flex;
            justify-content: space-between;
            margin: 50px;
        }

        .circle {
            width: 300px;
            height: 300px;
            margin-top: 50px;

        }

        input {
            width: 200px;
            height: 30px;
            border: 1px solid #e38a0e;
            outline: none;
            margin: 10px;
        }

        button {
            width: 100px;
            height: 30px;
            border: none;
            background-color: #e38a0e;
            color: white;
            margin: 10px;
        }

        ul {
            background-color: white;
            color: #e38a0e;
            width: 500px;
            list-style: none;
        }

        .left li {
            height: 50px;
            width: 500px;
            display: flex;
            border: 1px solid #e38a0e;
            align-items: center;
        }

        .left li span {
            display: inline-block;
            width: 100px;
            height: 50px;
            line-height: 50px;
            text-align: center;
        }

        li button {
            width: 90px;
            height: 40px;
            border: none;
            background-color: white;
            color: #e38a0e;
        }

        .right ul {
            width: 95px;
            margin: 40px 40px;
        }

        .right ul li {
            display: flex;
            justify-content: space-between;
            width: 95px;
            height: 30px;
        }

        .right ul li>span {
            display: inline-block;
            height: 20px;
            margin: 5px;
        }

        h3 {
            width: 150px;
            color: #e38a0e;
            margin-left: 70px;
            text-align: center;
            margin-top: 40px;
        }

        .red {
            color: red;
        }

        a {
            position: fixed;
            right: 0;
            bottom: 0;
            display: block;
            width: 100px;
            height: 30px;
            background-color: #e38a0e;
            color: #fff;
            text-align: center;
            line-height: 30px;
            text-decoration: none;
        }
    </style>
</head>

<body>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.3.2/dist/echarts.min.js"></script>
    <div id="app">
        <div class="left">
            <div class="first">
                <input type="text" placeholder="消费名称" v-model="Name">
                <input type="text" placeholder="消费价格" v-model.number="Price">
                <button @click="add">添加账单</button>
            </div>
            <ul>
                <li>
                    <span>编号</span>
                    <span>消费名称</span>
                    <span>消费价格</span>
                    <span v-model="data">消费时间</span>
                    <span>操作</span>
                </li>
                <li v-for="(item,index) in list" :key="item.id">
                    <span>{{index+1}}</span>
                    <span>{{item.name}}</span>
                    <span :class="{red:item.price>500}">{{item.price.toFixed(2)}}</span>
                    <span>{{data}}</span>
                    <span><button @click="del(item.id)">删除</button></span>
                </li>
                <li>
                    <span>消费总计:</span>
                    <span>{{sumPrice.toFixed(2)}}</span>
                </li>
            </ul>
        </div>
        <div class="right">

            <div style="width: 350px;">
                <h3>消费账单列表</h3>
                <div class="circle" ref="chart"></div>
            </div>
        </div>
    </div>
    <a href="首页.html">返回</a>
    <script>
        const { createApp } = Vue;

        createApp({
            data() {
                return {
                    list: [

                    ],
                    Name: '',
                    Price: '',
                    sum: 0,
                    data: '2023-07-25'
                };
            },
            methods: {
                add() {
                    if (this.Name == '' || this.Price == '') {
                        alert('请输入消费名称和消费价格');
                        return;
                    }
                    this.list.push({
                        id: this.list.length + 1,
                        name: this.Name,
                        price: this.Price,
                    });
                    this.data = new Date().toLocaleDateString();
                    this.Name = '';
                    this.Price = '';
                    this.renderChart();
                },
                del(id) {
                    this.list = this.list.filter(item => item.id !== id);
                    this.renderChart();
                },
                renderChart() {
                    const chartDom = this.$refs.chart;
                    const myChart = echarts.init(chartDom);
                    const option = {
                        title: {
                            text: '消费比例',
                            left: 'center'
                        },
                        tooltip: {
                            trigger: 'item'
                        },
                        series: [
                            {
                                name: '消费比例',
                                type: 'pie',
                                radius: '50%',
                                data: this.list.map(item => ({
                                    value: item.price,
                                    name: item.name
                                })),
                                emphasis: {
                                    itemStyle: {
                                        shadowBlur: 10,
                                        shadowOffsetX: 0,
                                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                                    }
                                }
                            }
                        ]
                    };
                    myChart.setOption(option);
                }
            },
            computed: {
                sumPrice() {
                    this.sum = 0;
                    this.list.forEach(item => {
                        this.sum += item.price;
                    });
                    return this.sum;
                }
            },
            mounted() {
                this.renderChart();
            }
        }).mount('#app');
    </script>
</body>

</html>